using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using net_work.Data.Entity.Cam;
using net_work.Data.Enum.Cam;

namespace net_work.Config.DataBaseConfig.Cam;

/// <summary>
/// 消息实体配置类
/// </summary>
public class MessageEntityTypeConfiguration : IEntityTypeConfiguration<Message>
{
    public void Configure(EntityTypeBuilder<Message> builder)
    {
        builder.ToTable("cam_mes");

        builder.HasKey(m => m.Id);

        // 配置字段名映射
        builder.Property(m => m.Id).HasColumnName("mes_id");
        builder.Property(m => m.Title).HasColumnName("msg_title").IsRequired().HasMaxLength(64);
        builder.Property(m => m.Content).HasColumnName("msg_cont").IsRequired().HasMaxLength(2048);

        // 将 MessageType 枚举存储为字符串
        builder.Property(m => m.Type)
            .HasColumnName("msg_type")
            .IsRequired()
            .HasMaxLength(32)
            .HasConversion(
                v => v.ToString(), // 枚举转字符串
                v => (MessageType)Enum.Parse(typeof(MessageType), v) // 字符串转枚举
            );

        // 将 MessageStatus 枚举存储为字符
        builder.Property(m => m.Status)
            .HasColumnName("msg_status")
            .IsRequired()
            .HasConversion(
                v => (char)v, // 枚举转字符
                v => (MessageStatus)v // 字符转枚举
            );

        builder.Property(m => m.ObjType).HasColumnName("obj_type").HasMaxLength(64)
            .HasConversion(
                v => v.ToString(), // 枚举转字符串
                v => string.IsNullOrEmpty(v)
                    ? null
                    : (MessageObjType)Enum.Parse(typeof(MessageObjType), v) // 字符串转枚举
            );
        builder.Property(m => m.ObjId).HasColumnName("obj_id");

        builder.Property(m => m.CreatedBy).HasColumnName("create_by").IsRequired().HasMaxLength(64);
        builder.Property(m => m.CreatedTime).HasColumnName("create_time").IsRequired();
        builder.Property(m => m.UpdatedBy).HasColumnName("update_by").IsRequired().HasMaxLength(64);
        builder.Property(m => m.UpdatedTime).HasColumnName("update_time").IsRequired();
        builder.Property(m => m.Remark).HasColumnName("remark").HasMaxLength(256);
        builder.Property(m => m.DelFlag).HasColumnName("del_flag").HasDefaultValue('0');
    }
}